/*
 * @Author: suoyue 445484430@qq.com
 * @Date: 2022-07-04 18:02:50
 * @LastEditors: suoyue 445484430@qq.com
 * @LastEditTime: 2022-07-04 18:08:47
 * @FilePath: \server\test\upload.js
 * @Description: 实现了上传图片的后端接口
 * 
 * 
 * Copyright (c) 2022 by suoyue 445484430@qq.com, All Rights Reserved. 
 */
// 引入express
const express = require("express")
// 引入路径处理模块
let path = require('path');
// 导入 cors 中间件
const cors = require('cors')
const util=require('../utils/index')

const app = express()
// 监听6001端口
app.listen(6001, () => {
    console.log("open '127.0.0.1:6001'")
})

// 将 cors 注册为全局中间件
app.use(cors())
app.use(express.json())
app.use(express.urlencoded({ extended: false }))


app.get('/', (req, res) => {
    console.log("hello world")
    res.send({
        msg: "haha"
    })
})

//添加商品
app.post("/product",util.upload.single("file"),(req,res)=>{
    console.log(req.body)
    //接收普通文本参数
    let {name} = req.body;
    //接收上传文件数据 
    let imgUrl
    if(req.file){
        imgUrl = req.file.filename;
    }else{
        //error 无图片处理
        console.log("error no image")
    }
    console.log(imgUrl)
    res.send({imgUrl,name})
})
